<?php
namespace App\Response;

use Dingo\Api\Transformer\Factory;
use App\Http\Controllers\BaseController;

class Response extends BaseController{

    /**
     * Notes: 返回json标准数据
     * Author: chenjie
     * @param array $data
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     * Date: 2021/6/7
     */
    public static function sendJsonData(array $data, $msg='')
    {
        $arr = [
            'code' => 200,
            'data' => $data,
            'msg' => $msg,
        ];

        return response()->json($arr);
    }

    /**
     * 普通响应方法,返回整个数据
     * @param $data
     * @return string
     */
    static function sendData($data,$info='')
    {
        $array = [
            'code' => 200,
            'data' => $data,
            'message' => $info,
        ];

        return json_encode($array);
    }
    /**
     * 普通响应方法,返回整个数据
     * @param $data
     * @return string
     */
    static function sendDataNew($data)
    {
        return response()->json($data);
    }

    /**
     * 响应列表数据
     * @param $data
     * @param array $param
     * @return string
     */
    static function sendList($data,$param=array(), $ext = [])
    {
        $list = $data->items();
        $next = $data->nextPageUrl();
        $page = $data->currentPage();
        $limit = $data->perPage();
//        $total = $data->total();

        if(!empty($next)) {

            $next_arr = explode('?',$next);

            if(!empty($param)) {

                $next = $next_arr[0]."?page".$next_arr[1].'&'.http_build_query($param);

            } else {

                $next = $next_arr[0]."?page".$next_arr[1];
            }
        }

        $array = [
            'code' => 200,
            'data' => [
                'list' => $list,
                'meta' => [
                    'next' => $next,
                    'page' => $page,
                    'limit' => $limit,
//                    'total' => $total
                ],
                "head" => $ext
            ]
        ];

        return json_encode($array);
    }


    /**
     * 发送H5的列表数据(展示页数)
     * @param $data
     * @param array $param
     * @return string
     */
    static function sendWebList($data,$param=array())
    {
        $list = $data->items();
        $next = $data->nextPageUrl();
        $pages = $data->lastPage();

        if(!empty($next)) {

            $next_arr = explode('?',$next);

            if(!empty($param)) {

                $next = $next_arr[0]."?page".$next_arr[1].'&'.http_build_query($param);

            } else {

                $next = $next_arr[0]."?page".$next_arr[1];
            }
        }

        $array = [
            'code' => 200,
            'data' => [
                'list' => $list,
                'meta' => [
                    'next' => $next,
                    'pages' => $pages
                ]
            ]
        ];

        return json_encode($array);
    }

    /**
     * Ajax方式返回数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type AJAX返回数据格式
     * @param int $json_option 传递给json_encode的option参数
     * @return void
     */
    public static function ajaxReturn($data,$type='',$json_option=0) {
        if(empty($type)) $type  =   'JSON';
        switch (strtoupper($type)){
            case 'JSON' :
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data,$json_option));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
            case 'EVAL' :
                // 返回可执行的js脚本
                header('Content-Type:text/html; charset=utf-8');
                exit($data);
            default     :
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data,$json_option));
        }
    }

    public static function fmt($status = -2, $info = '', $data = '') {
        if ($status == -2) {
            exit('参数调用错误');
        }

        return ['status' => $status, 'info' => $info, 'data' => $data];
    }
}

?>
